//
//  CJPFullController.swift
//  CJPack
//
//  Created by 陈警卫 on 2023-06-13.
//

import UIKit

class CJPFullController: UIViewController {
    
    var didDismissAction: (() -> ())?
    
    var player: CJPlayer?
    var customView: UIView!
    var controlView: CJPControlView!
    
    var isDismissFull: Bool = false
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        setupView()
        setupControl()
        setupData()
    }
    
    func setupView() {
        self.customView = UIView(frame: self.view.bounds)
        self.customView.backgroundColor = .black
        self.view.addSubview(customView)
        
        guard let player = player else { return }
        player.playerLayer?.frame = self.customView.bounds
        self.customView.layer.addSublayer(player.playerLayer!)
    }
    
    func setupControl() {
        self.controlView = CJPControlView(frame: self.view.bounds)
        self.view.addSubview(self.controlView)
        self.controlView.orientation = .portfull
        self.controlView.time = self.player?.timeString
        self.controlView.player = self.player
        
        /// 全屏事件
        self.controlView.didFullAction = { [weak self] in
            self?.dismissFullAction()
        }
    }
    
    func setupData() {
        self.controlView.isPlay = (self.player?.isPlay) ?? false
        self.controlView.progress = self.player?.progress
    }
    
    func setupFrame() {
        if self.isDismissFull {
            return
        }
        self.customView.frame = self.view.bounds
        self.player?.playerLayer?.frame = self.customView.bounds
        self.controlView.frame = self.view.bounds
        
        let orientation = UIDevice.current.orientation
        if orientation == .landscapeRight || orientation == .landscapeLeft {
            self.controlView.orientation = .landfull
        }
        
    }
    
    deinit {
        kCJLog("\(self)--deinit")
    }
}

extension CJPFullController {
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
    }
    
    override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)
        CJOrientScreen().switchScreenOrientation(vc: self, orientation: .auto)
    }
    
    override func viewWillDisappear(_ animated: Bool) {
        super.viewWillDisappear(animated)
    }
    
    override func viewDidDisappear(_ animated: Bool) {
         super.viewDidDisappear(animated)
    }
}

extension CJPFullController {
    
    func dismissFullAction() {
        CJOrientScreen().switchScreenOrientation(vc: self, orientation: .port)
        self.isDismissFull = true
        if let didDismissAction = self.didDismissAction {
            didDismissAction()
        }
        
    }
    
    override func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator) {
        coordinator.animate { [weak self] context in
            self?.setupFrame()
        }
        super.viewWillTransition(to: size, with: coordinator)
    }
    
    
    
    
    
}
